<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//
require_once('Gecko/Form/Renderer/Interface.php');

/**
 * Simple Form Renderer, that draws data in
 * a table
 *
 * @package Gecko.Form.Renderer;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v1.0$ 2008
 * @access public
 **/
class Gecko_Form_Renderer_Table implements Gecko_Form_Renderer_Interface {
	/**
	 * The Form Table
	 *
	 * @see Gecko_Table
	 */
	private $table;

	/**
	 * The code to start the form
	 */
	private $start;

	/**
	 * The code to end the form
	 */
	private $end;

	/**
	 * The HTML of the form
	 */
	private $html;

	/**
	 * Creates a new instance of the renderer
	 *
	 * @param array The Table Properties
	 */
	public function __construct($tableProps = array()) {
		$this->table = new Gecko_Table($tableProps);
	}

	/**
	 * Starts drawing the form
	 *
	 * @param string The form name
	 * @param string Form url
	 * @param string Form Method
	 * @param string The Form encoding type
	 */
	public function beginForm($name, $url, $method, $enctype) {
		$this->start = "<form name=\"$name\" action=\"$url\" id=\"$name\" method=\"$method\" enctype=\"$enctype\">";
	}

	/**
	 * Ends the form
	 */
	public function endForm() {
		$this->end = "</form>";
	}

	/**
	 * Adds a new Item into the table
	 *
	 * @param string The form name
	 * @param string The form html code
	 * @param array The field error messages
	 */
	public function addItem( $name, $html, $messages = null ) {
		$this->table->addRow();
		$this->table->addCell( ucfirst( $name ) );
		if( $messages ) {
			$html = "<span class=\"field-error\">$html $messages</span";
		}
		$this->table->addCell( $html );
	}

	/**
	 * Adds HTML code to the form
	 *
	 * @param string The html code
	 */
	public function addHTML( $html ) {
		$this->html .= $html;
	}

	/**
	 * Gets the build form using PHP __toString method
	 *
	 * @return string
	 */
	public function __toString() {
		return $this->start . $this->table . $this->html . $this->end;
	}
}
?>